var _processInstanceId = "";
var _taskId = "";
var _taskName = "";
var _leaveApplicationId = 0;
var _startUserId = 0;

var thisPage = {
    // 初始化页面
    OnPageLoad: function () {
        //$('#loading').hide();
        thisPage.FetchByKey();
    },
    FetchByKey: function () {
        let variables = {};
        variables['LeaveApplicationId'] = _leaveApplicationId;
        $.ajax({
            type: "POST",
            url: parent.basePath + "/hr/leave_application/fetchByKey.html",
            timeout: 30 * 1000,
            data: JSON.stringify(variables),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            let _item = data.LeaveApplicationEntity;
            _startUserId = _item.userId;
            $("#LeaveApplyUserName").text(_item.userName);
            $("#LeaveApplicationTypeName").text(_item.leaveApplicationTypeName);
            $("#StartTime").text(_item.startTime);
            $("#EndTime").text(_item.endTime);
            $("#Reason").text(_item.reason);
            if (data.ProcApproveComment !== null) {
                thisPage.ShowApproveComment(data.ProcApproveComment);
            }
        }).fail(function (data) {
            $('#loading').hide();
            ajaxError(data, 1);
        }).always(function () {
            $('#loading').hide();
        });
    },
    ShowApproveComment: function (result) {
        $("#tabDataComment tbody").empty();
        let content_01 = '';
        for (let i = 0; i < result.length; i++) {
            content_01 += '<tr _mash_id="' + result[i].taskId + '">';
            content_01 += '<td>' + result[i].taskName + '</td>';
            content_01 += '<td>' + result[i].assigneeName + '</td>';
            content_01 += '<td>' + formatNullData(result[i].endTime) + '</td>';
            content_01 += '<td style="border-right:none;">' + formatNullData(result[i].approveComment) + '</td>';
            content_01 += '</tr>';

            $("#tabDataComment tbody").append(content_01);
            content_01 = '';
        }
        setOneTableRowColor("tabDataComment");
    },
    HandleTask: function (approve) {
        let variables = {};
        variables['TaskId'] = _taskId;
        variables['ProcessInstanceId'] = _processInstanceId;
        variables['ApprovUserId'] = parseInt(parent.$("#UserId").val()); // 审核人
        variables['StartUserId'] = parseInt(_startUserId);               // 流程发起人
        variables['Approve'] = approve;
        if (_taskName === "部门审批") {
            variables['FlowName'] = "部门审批";
        }
        if (_taskName === "人事审批") {
            variables['FlowName'] = "人事审批";
        }
        if (approve) {
            variables['Comment'] = $("#Comment").val() === "" ? "同意" : $("#Comment").val();
        } else {
            variables['Comment'] = $("#Comment").val() === "" ? "驳回" : $("#Comment").val();
        }
        $.ajax({
            type: "POST",
            url: parent.basePath + "/hr/leave_application/handleTask.html",
            timeout: 30 * 1000,
            data: JSON.stringify(variables),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            // 刷新父窗口数据
            parent._Instance.FetchForPage(parent._Instance.$pagination.attr("_msh_page"));
            // 关闭对话框
            closePopupWnd();
        }).fail(function (data) {
            $('#loading').hide();
            ajaxError(data, 1);
        }).always(function () {
            $('#loading').hide();
        });
    }
};

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    _processInstanceId = jQuery.url.param("processInstanceId") === undefined ? "" : jQuery.url.param("processInstanceId");
    _taskId = jQuery.url.param("taskId") === undefined ? "" : jQuery.url.param("taskId");
    _taskName = jQuery.url.param("taskName") === undefined ? "" : jQuery.url.param("taskName");
    _leaveApplicationId = jQuery.url.param("bid") === undefined ? 0 : jQuery.url.param("bid");

    $(".accordion-set > span").on("click", function () {
        if ($(this).hasClass("active")) {
            $(this).removeClass("active");
            $(this).siblings(".accordion-content").slideUp(200);
            $(".accordion-set > span i").removeClass("fa-minus").addClass("fa-plus");
        } else {
            $(".accordion-set > span i").removeClass("fa-minus").addClass("fa-plus");
            $(this).find("i").removeClass("fa-plus").addClass("fa-minus");
            $(".accordion-set > span").removeClass("active");
            $(this).addClass("active");
            $(".accordion-content").slideUp(200);
            $(this).siblings(".accordion-content").slideDown(200);
        }
    });

    thisPage.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
}
